<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>方案新增或修改页面</title>
    <link rel="stylesheet" href="../../res/layui/css/layui.css" media="all">
    <!-- 注意：如果你直接复制所有代码到本地，上述js路径需要改成你本地的 -->
    <script src="../../res/js/jquery-3.1.1.min.js" type="text/javascript" charset="utf-8"></script>
    <script src="../../res/layui/layui.js"></script>
    <script src="../../res/js/Tools.js"></script>
    <script>
        $.ajaxSetup({
            async: false
        });
    </script>
</head>
<body>
<div id="container">
<form class="layui-form" action="" id="Leader">
    <input type="hidden" name="schemeId" id="schemeId" lay-verify="schemeId" autocomplete="off" class="layui-input">
    <input type="hidden" name="schemeType" id="schemeType" lay-verify="schemeType" autocomplete="off" class="layui-input">
    <input type="hidden" name="file1" id="file1" lay-verify="file1" autocomplete="off" class="layui-input">
    <input type="hidden" name="file2" id="file2" lay-verify="file2" autocomplete="off" class="layui-input">
    <input type="hidden" name="file3" id="file3" lay-verify="file3" autocomplete="off" class="layui-input">
    <input type="hidden" name="resultFile" id="resultFile" lay-verify="resultFile" autocomplete="off" class="layui-input">
    <input type="hidden" name="remark" id="remark" lay-verify="remark" autocomplete="off" class="layui-input">
    <div class="layui-form-item"></div>
    <div class="layui-form-item">
        <label class="layui-form-label">方案名称</label>
        <div class="layui-input-block">
            <input class="layui-input" type="text" name="schemeName" id="schemeName" required
                   lay-verify="required" placeholder="方案名称" autocomplete="off"/>
        </div>
    </div>
    <div class="layui-form-item">
        <label class="layui-form-label">方案年份</label>
        <div class="layui-input-block">
            <input class="layui-input" type="text" name="year" id="year" required
                   lay-verify="required" placeholder="方案年份" autocomplete="off"/>
        </div>
    </div>
    <div class="layui-form-item">
        <label class="layui-form-label">方案月份</label>
        <div class="layui-input-block">
            <input class="layui-input" type="text" name="month" id="month" required
                   lay-verify="required" placeholder="方案月份" autocomplete="off"/>
        </div>
    </div>
    <div class="layui-form-item">
        <label class="layui-form-label">运动员</label>
        <div class="layui-input-block" id="yundongyuan">
        </div>
    </div>
    <div class="layui-form-item">
        <label class="layui-form-label">附件一</label>
        <button type="button" class="layui-btn" id="test1"><i class="layui-icon"></i>上传文件</button><span id="fileUrl1"></span>
    </div>
    <div class="layui-form-item">
        <label class="layui-form-label">附件二</label>
        <button type="button" class="layui-btn" id="test2"><i class="layui-icon"></i>上传文件</button><span id="fileUrl2"></span>
    </div>
    <div class="layui-form-item">
        <label class="layui-form-label">附件三</label>
        <button type="button" class="layui-btn" id="test3"><i class="layui-icon"></i>上传文件</button><span id="fileUrl3"></span>
    </div>
    <label class="layui-form-label"></label>
    <div class="layui-form-item">
        <div class="layui-input-block">
            <button class="layui-btn site-demo-active" lay-submit lay-filter="formDemo" >立即提交</button>
            <button type="reset" class="layui-btn layui-btn-primary">重置</button>
        </div>
    </div>
</form>
</div>
<style>
    body { font-family:Verdana; margin:0 auto;}
    #container {margin: 0 auto; width:600px;}
</style>
<script>
    //取到上一个页面传过来的id

    var schemeId = getQueryString("id");
    var remark=getQueryString("remark");
    $("#remark").val(remark);
    var str="";
    layui.use(['form', 'layedit', 'laydate','tree','element','upload'], function () {
        var form = layui.form
            , layer = layui.layer
            , layedit = layui.layedit
            , laydate = layui.laydate
            , tree = layui.tree
            , element = layui.element
            , upload = layui.upload;

        //附件一
        upload.render({
            elem: '#test1'
            ,url: '../../AtsSchemeFsController/uploadFile'
            ,accept: 'file' //普通文件
            ,done: function(res){
                $("#fileUrl1").text(res.msg);
                $("#file1").val(res.msg);
            }
        });

        //附件二
        upload.render({
            elem: '#test2'
            ,url: '../../AtsSchemeFsController/uploadFile'
            ,accept: 'file' //普通文件
            ,done: function(res){
                $("#fileUrl2").text(res.msg);
                $("#file2").val(res.msg);
            }
        });

        //附件三
        upload.render({
            elem: '#test3'
            ,url: '../../AtsSchemeFsController/uploadFile'
            ,accept: 'file' //普通文件
            ,done: function(res){
                $("#fileUrl3").text(res.msg);
                $("#file3").val(res.msg);
            }
        });


        //日期
        laydate.render({
            elem: '#date'
        });
        laydate.render({
            elem: '#date1'
        });

        //查询运动员
        var url = "../../AtsSchemeFsController/selectAthlete";
        var data = null;
        $.post(url,data,function (obj) {
            $.each(obj,function(index,item){
                $("#yundongyuan").append("<input type='checkbox' name='yundong' id='"+item.athleteId+"' value='"+item.athleteId+"' title='"+item.athleteName+"'/>");

                if(schemeId!=null&&schemeId!=""){
                    var urlType="../../AtsSchemeFsController/selectCheckAthlete";
                    var dateType={schemeId:schemeId};
                    $.post(urlType,dateType,function(objType){
                        $.each(objType,function(indexs,items){
                            if(items.athleteId==item.athleteId){
                                $("#"+item.athleteId+"").attr("checked","");
                            }
                        });
                        form.render();
                    },"json");
                }
            });
            form.render();
        },"json");
        if (schemeId!=null&&schemeId!="") {
            var strs="";
            var url = "../../AtsSchemeFsController/selectByPrimaryKey";
            var data = {schemeId: schemeId};
            $.post(url, data, function (obj) {
                $("#schemeId").val(obj.schemeId);
                $("#schemeName").val(obj.schemeName);
                $("#year").val(obj.year);
                $("#month").val(obj.month);
                $("#schemeType").val(obj.schemeType);
                $("#remark").val(obj.remark);
                $("#sort").val(obj.sort);
                $("#fileUrl1").text(obj.file1);
                $("#file1").val(obj.file1);
                $("#fileUrl2").text(obj.file2);
                $("#file2").val(obj.file2);
                $("#fileUrl3").text(obj.file3);
                $("#file3").val(obj.file3);

            }, "json");
        };

        //自定义验证规则
        form.verify({
            schemeName: function(value){
                if(!value.length){
                    return '方案名称不能包含空格';
                }
            },
            file1: function(value){
                if(!value.length){
                    return '至少上传一个附件';
                }
            }
        });

        //监听提交
        form.on('submit(formDemo)', function (data) {
            // 单击之后提交按钮不可选,防止重复提交
            var DISABLED = 'layui-btn-disabled';
            // 增加样式
            $('.site-demo-active').addClass(DISABLED);
            // 增加属性
            $('.site-demo-active').attr('disabled', 'disabled');
            var url = "../../AtsSchemeFsController/addAndUpdate";
            var pasChecked=$("input[name='yundong']:checked");
            //所有选中的复选框
            var str="";
            for(var i=0;i<pasChecked.length;i++){
                var items=pasChecked[i].value;
                str+=items+",";
                console.info(str);
            }
            data.field.remark=str;

            $.ajax({
                url: url,
                type: "post",
                data:data.field,
                dataType:"json",
                success:function(obj){
                    if (obj.num == 1) {
                        layer.msg(obj.msg,{
                            icon: 1,
                            time: 1000,
                        });
                    }else{
                        layer.msg(obj.msg, {
                            icon: 2,
                            time: 1000,
                        });
                    }
                    window.parent.location.reload();//刷新父页面
                    var index = parent.layer.getFrameIndex(window.name);
                    parent.layer.close(index);
                }, error: function (e) {
                    alert('失败了');
                }

            });
        });
    });

</script>

</body>
</html>